package com.meida.module.bus.provider.controller;


import com.alibaba.fastjson.util.TypeUtils;
import com.google.common.collect.Maps;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.springmvc.base.BaseController;
import com.meida.common.utils.FlymeUtils;
import com.meida.module.bus.client.entity.BusPurchase;
import com.meida.module.bus.client.entity.BusSupplier;
import com.meida.module.bus.client.entity.BusVersion;
import com.meida.module.bus.client.entity.srmapi.basedata.SrmContracts;
import com.meida.module.bus.provider.service.BusPurchaseDetailsService;
import com.meida.module.bus.provider.service.BusPurchaseService;
import com.meida.module.bus.provider.service.BusSupplierService;
import com.meida.module.bus.provider.service.BusVersionService;
import com.meida.module.bus.provider.service.srmservice.SrmSqlserCommonService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/bus/purchase/")
public class BusPurchaseController  extends  BaseController<BusPurchaseService,BusPurchase>{
    @Autowired
    private BusVersionService busVersionService;
    @Autowired
    private BusPurchaseDetailsService busPurchaseDetailsService;
    @Autowired
    private BusSupplierService busSupplierService;
    @Autowired
    private SrmSqlserCommonService srmSqlserCommonService;

    /**
     * 查询采购申请记录页面
     * @param params
     * @return
     */
    @ApiOperation(value = "采购申请-分页列表", notes = "采购申请")
    @GetMapping(value = "page")
    public ResultBody pageList(@RequestParam(required = false) Map params)
    {
        BusVersion lastVersion=busVersionService.getLatestVersion(Long.parseLong(params.get("projectId").toString()),1);
        if(FlymeUtils.isNotEmpty(lastVersion)){
            String bigVersion=lastVersion.getBigVersion();
            Integer bigVal= TypeUtils.castToInt(bigVersion);
            if(bigVal==100){
                params.put("versionType","1");
            }else{
                params.put("versionType","2");
            }
        }
        return bizService.pageList(params);
    }
    @ApiOperation(value = "获取字典类型", notes = "获取字典类型")
    @GetMapping(value = "getCaseTypeDate")
    public ResultBody getCaseTypeDate(@RequestParam(required = false) Map params) {
        List<EntityMap> entityMapList = busPurchaseDetailsService.diclist("docTypes");//单据类型
        return ResultBody.ok().data(entityMapList);
    }

    @ApiOperation(value = "获取所有供应商", notes = "获取所有供应商")
    @GetMapping(value = "getallSupplier")
    public ResultBody getallSupplier(@RequestParam(required = false) Map params) {
        List<BusSupplier> entityList = busSupplierService.getallSupplier();
        return ResultBody.ok().data(entityList);
    }

    @ApiOperation(value = "获取未同步数据", notes = "获取未同步数据")
    @PreAuthorize("system:user:list")
    @PostMapping(value = "getSyncSrmOpenData")
    public ResultBody getSyncSrmOpenData(@RequestParam(required = false) Map params) {
        ResultBody resultBody = bizService.getSyncSrmOpenData(params);
        if(null != resultBody.getData()) {
            List<EntityMap> entityMapList = (List<EntityMap>) resultBody.getData();
            entityMapList.stream().forEach(entiObj->{
                Long contractsId = entiObj.getLong("contractsId");
                if(null != contractsId) {
                    Map<String, Object> searchMap = Maps.newHashMap();
                    searchMap.put("contractsId", contractsId);
                    SrmContracts srmContracts = srmSqlserCommonService.getSrmContract(searchMap);
                    entiObj.put("vendorSrm", srmContracts.getVendorName());
                }

            });
        }
        return resultBody;
    }

    @PostMapping(value = "syncDataOpen")
    public ResultBody syncDataOpen(@RequestParam(required = false) Map params) throws Exception {
        return bizService.syncDataOpen(params);
    }
}
